﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.WebPages;
using System.IO;
using MvcJE.Core.Inet;


using System.Data.Entity;

namespace MvcJE.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

        protected void Application_Start()
        {
            Database.SetInitializer<IPServiceDb>(new IPServiceInitializer());

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //BundleTable.Bundles.RegisterTemplateBundles();
            BundleTable.Bundles.EnableDefaultBundles();
            // customization starts here
            DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("iPhone")
            {
                ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf
                    ("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)
            });

            DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("android")
            {
                ContextCondition = (context => context.Request.UserAgent.IndexOf
                    ("android", StringComparison.OrdinalIgnoreCase) > -1)
            });

        }

    }

    public class IPServiceInitializer : CreateDatabaseIfNotExists<IPServiceDb>
    {
        protected override void Seed(IPServiceDb context)
        {
            var catId = Guid.NewGuid();
            var categories = new List<MopModuleCategory>
            {
                new  MopModuleCategory {
                     Name="Core",
                      MopModuleCategoryId = catId 
                }
            };
            categories.ForEach(d => context.MopModuleCategory.Add(d));
            var confId = Guid.NewGuid();
            var group = new List<MopModuleGroup>
            {
                new MopModuleGroup{
                     Name="Device Configurations",
                      MopModuleGroupId= confId,
                      MopModuleCategoryId =catId
                },
                                new MopModuleGroup{
                     Name="Service Configurations",
                      MopModuleGroupId =Guid.NewGuid(),
                      MopModuleCategoryId =catId
                },
                                new MopModuleGroup{
                     Name="Troubleshooting",
                      MopModuleGroupId =Guid.NewGuid(),
                      MopModuleCategoryId =catId
                }
            };
            group.ForEach(g => context.MopModuleGroup.Add(g));
            var t = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "App_Data\\DataStore\\Templates\\Demo1.txt");
            var mop=new List<MopModule>
            {
                new MopModule {
                     MopModuleId =Guid.NewGuid(),
                     DateCreated=DateTime.Now,
                     DateUpdated=DateTime.Now,
                     MopModuleGroupId =confId,
                      Title="Feature Demo 1",
                     Template=t.Replace("\n","\r\n")
                }
            };
            mop.ForEach(m=>context.MopModule.Add(m));
            context.SaveChanges();
        }
    }



}